//index.js
//获取应用实例
//引用腾讯地图API
const http = require('../js/ajax.js')
const api = require('../js/api.js')
//var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
//var qqmapsdk;

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: "",
    src: "",
    name:"",
    cun:""
  },

  onChangeAddress: function (e) {
    // wx.navigateTo({
    //   url: "../position/position"
    // });
    console.log(e)
    let that = this;
    wx.chooseLocation({
      success: function(res) {
        console.log(res)
        that.setData({
          ['name.lng']: res.longitude,
          ['name.lat']: res.latitude,
          ['name.mapAddress']: res.address,
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('item++++++',options.item)
    console.log('global++++',app.globalData.shouhuo)
    this.setData({
      name: app.globalData.shouhuo,
      //address: app.globalData.shouhuo.mapAddress ,
    })
    //console.log(this.data.address)
    //console.log(this.data.name)
    /*判断是第一次加载还是从position页面返回
   如果从position页面返回，会传递用户选择的地点*/
    // if (options.address != null && options.address != '') {
    //   //设置变量 address 的值
    //   this.setData({
    //     address: options.address
    //   });
    // } else {
    //   // 实例化API核心类
    //   qqmapsdk = new QQMapWX({
    //     //此key需要用户自己申请
    //     key: 'HbUVYMUg6PwbOnXkztdgSQlQ'
    //   });
    //   var that = this;
    //   // 调用接口
    //   qqmapsdk.reverseGeocoder({
    //     success: function (res) {
    //       that.setData({
    //         address: res.result.address
    //       });
    //     },
    //     fail: function (res) {
    //       //console.log(res);
    //     },
    //     complete: function (res) {
    //       //console.log(res);
    //     }
    //   });
    // }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.replace()
  },
  ss(e){
    //console.log('666',e);
    // this.data.name.sex = 2
    this.setData({
      ['name.sex']: 2
    })
    //console.log(this.data.name.sex)
  },
  sss(e) {
    //console.log('666', e);
    // this.data.name.sex = 1
    this.setData({
      ['name.sex']: 1
    })
    //console.log(this.data.name.sex)
  },
  //获取input输入的内容
  getName(e){
    let contacts = e.detail.value;
    this.setData({
      ['name.contacts']: contacts
    })
  },
  getPhone:function(e){
    let contacts = e.detail.value;
    this.setData({
      ['name.contactsMobile']:contacts
    })
  },
  getAdd: function (e) {
    let contacts = e.detail.value;
    this.setData({
      ['address']: contacts
    })
  },
  getHome: function (e) {
    let contacts = e.detail.value;
    this.setData({
      ['name.detailAddress']: contacts
    })
  },
  preserve(e){
    // wx.navigateBack({
    //   url:'../address/address'
    // })
    // wx.showToast({
    //   title: '成功',
    //   icon: 'success',
    //   duration: 1200
    // })
    // setTimeout(function () {
    //   wx.hideLoading()
    // }, 2000)  !(/^1[123456789]\d{9}$/.test(this.data.name.contactsMobile))

    //console.log(this.data.name.contacts.length)
    if (this.data.name.contacts.length > 2 && this.data.name.contacts.length < 20){
      //console.log('1')
      if (/^1[123456789]\d{9}$/.test(this.data.name.contactsMobile)){
        if (!(this.data.name.mapAddress=='')){
          if (!(this.data.name.detailAddress=='')){
            http(api.rep, "", {
              "memberId": 589,
              "createUser": "2190380",
              "memberSource": "601",
              "contacts": this.data.name.contacts,
              "sex": this.data.name.sex,
              "contactsMobile": this.data.name.contactsMobile,
              "detailAddress": this.data.name.detailAddress,
              "lng": this.data.name.lng,
              "lat": this.data.name.lat,
              "mapAddress": this.data.name.mapAddress,
              "isDefault": 0,
              "memberAddressId": this.data.name.memberAddressId
            }, "post").then(res => {
              //console.log(1,res)
              if (res.data.status.statusCode === 0 && res.data.result) {
                // this.setData({
                //   cun: e.currentTarget.dataset.item
                // })
                wx.showToast({
                  title: '保存成功',
                  icon: 'none'
                })
                setTimeout(function () {
                  wx.navigateBack()
                }, 1200)
              } else {
                wx.showToast({
                  title: '保存失败',
                  icon: 'none'
                })
              }
            })
          }else{
            wx.showToast({
              title: '楼牌号不能为空',
              icon: 'none',
              duration: 1200
            })
            setTimeout(function () {
              wx.hideLoading()
            }, 2000)
          }
        }else{
          wx.showToast({
            title: '收获地址不能为空',
            icon: 'none',
            duration: 1200
          })
          setTimeout(function () {
            wx.hideLoading()
          }, 2000)
        }
      }else{
        wx.showToast({
          title: '手机格式错误',
          icon: 'none',
          duration: 1200
        })
        setTimeout(function () {
          wx.hideLoading()
        }, 2000)
      }
    }else{
      wx.showToast({
        title: '联系人格式错误',
        icon: 'none',
        duration: 1200
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 2000)
    }
    
  },
  delete(){
    http(api.deletes, "params", {
      addressId: this.data.name.memberAddressId
    }).then(res => {
      console.log("删除", res)
      if (res.data.status.statusCode === 0 && res.data.result) {
        // this.setData({
        //   cun: e.currentTarget.dataset.item
        // })
        wx.showToast({
          title: '删除成功',
          icon: 'none'
        })
        setTimeout(function () {
          wx.navigateBack()
        }, 1200)
      } else {
        wx.showToast({
          title: '删除失败',
          icon: 'none'
        })
      }
    })
    // wx.showModal({
    //   title: '提示',
    //   content: '确认要删除么',
    //   success(res) {
    //     let that = this;
    //     if (res.confirm) {
    //       console.log('用户点击确定')
    //       http(api.deletes,"params",{
    //         addressId: that.data.name.memberAddressId
    //       }).then(res=>{
    //         console.log("删除",res)
    //       })
    //       // wx.navigateBack({
    //       //   url:'../address/address'
    //       // })
    //       // this.setData({
    //       //   name: '',
    //       //   address: '',
    //       // })
    //     } else if (res.cancel) {
    //       //console.log('用户点击取消')
    //     }
    //   }
    // })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})